package com.hlhj.funlike_business.activity.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.activity.OrderDetailAty;
import com.hlhj.funlike_business.bean.OrderListBean;
import com.hlhj.funlike_business.utils.Tools;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class AllOrderAdp extends BaseQuickAdapter<OrderListBean.DataBeanX.DataBean,BaseViewHolder>{
    public AllOrderAdp(@Nullable List<OrderListBean.DataBeanX.DataBean> data) {
        super(R.layout.adp_orderitem,data);
    }

    @Override
    protected void convert(BaseViewHolder helper, final OrderListBean.DataBeanX.DataBean item) {
        CircleImageView icon = helper.getView(R.id.img_icon);
        TextView tv_address = helper.getView(R.id.tv_address);
        TextView tv_name = helper.getView(R.id.tv_name);
        TextView tv_phone = helper.getView(R.id.tv_phone);
        TextView tv_allfood = helper.getView(R.id.tv_allfood);
        TextView tv_total_price = helper.getView(R.id.tv_total_price);
        TextView tv_backorder = helper.getView(R.id.tv_backorder);
        final TextView tv_takeorder = helper.getView(R.id.tv_takeorder);
        TextView tv_more = helper.getView(R.id.tv_more);
        TextView tv_dot = helper.getView(R.id.tv_dot);
        TextView tv_status = helper.getView(R.id.tv_status);
        RecyclerView order_recy = helper.getView(R.id.order_recy);
        RelativeLayout rl_orderitem = helper.getView(R.id.rl_orderitem);

        Glide.with(mContext).load(item.getAddress().getImg()).into(icon);
        tv_address.setText(item.getAddress().getAddress_detail());
        tv_name.setText(item.getAddress().getName());
        tv_phone.setText(item.getAddress().getMobile());
        tv_allfood.setText("共" + item.getGoods_total_num() + "件商品");
        tv_total_price.setText("￥" + item.getPrice());
        if (item.getStatus()==20){
            tv_takeorder.setText("立即接单");
            tv_status.setText("待接单");
            tv_takeorder.setTextColor(mContext.getResources().getColor(R.color.white));
            tv_takeorder.setBackground(mContext.getResources().getDrawable(R.drawable.jiedan_bg));
        }else if (item.getStatus()==21){
            tv_takeorder.setText("立即配送");
            tv_takeorder.setTextColor(mContext.getResources().getColor(R.color.white));
            tv_takeorder.setBackground(mContext.getResources().getDrawable(R.drawable.peisong_bg));
            tv_status.setText("已接单");
        }else if (item.getStatus()==30){
            tv_takeorder.setText("配送中");
            tv_status.setText("已接单");
            tv_takeorder.setTextColor(mContext.getResources().getColor(R.color.white));
            tv_takeorder.setBackground(mContext.getResources().getDrawable(R.drawable.jiedan_bg));
        }else if (item.getStatus()==40){
            tv_takeorder.setText("查看详情");
            tv_backorder.setVisibility(View.GONE);
            tv_status.setText("已完成");
            tv_takeorder.setTextColor(mContext.getResources().getColor(R.color.white));
            tv_takeorder.setBackground(mContext.getResources().getDrawable(R.drawable.jiedan_bg));
        }else if (item.getStatus()==60){
            tv_takeorder.setText("查看详情");
            tv_backorder.setVisibility(View.GONE);
            tv_status.setText("已退单");
            tv_takeorder.setTextColor(mContext.getResources().getColor(R.color.white));
            tv_takeorder.setBackground(mContext.getResources().getDrawable(R.drawable.jiedan_bg));
        }

        tv_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Tools.callPhone(mContext,item.getAddress().getMobile());
            }
        });
        rl_orderitem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, OrderDetailAty.class);
                intent.putExtra("order_id",item.getId());
                mContext.startActivity(intent);
            }
        });
        tv_backorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (click!=null){
                    click.showChargeBack(item.getId());
                }
            }
        });
        tv_takeorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_takeorder.getText().toString().equals("查看详情")){
                    Intent intent = new Intent(mContext, OrderDetailAty.class);
                    intent.putExtra("order_id",item.getId());
                    mContext.startActivity(intent);
                }else if (tv_takeorder.getText().toString().equals("立即配送")){
                    if (listener!=null){
                        listener.showResult(item.getId(),30);
                    }
                }else if (tv_takeorder.getText().toString().equals("立即接单")){
                    if (listener!=null){
                        listener.showResult(item.getId(),21);
                    }
                }
            }
        });

        if (item.getGoods().size()>10){
            tv_more.setVisibility(View.VISIBLE);
        }else {
            tv_more.setVisibility(View.GONE);
        }
        OrderItemAdp orderItemAdp = new OrderItemAdp(item.getGoods());
        order_recy.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
        order_recy.setAdapter(orderItemAdp);
    }

    public interface getResult{
        void showResult(int order_id,int status);
    }

    public getResult listener;

    public void setListener(getResult listener) {
        this.listener = listener;
    }

    public interface getChageBack{
        void showChargeBack(int order_id);
    }

    public getChageBack click;

    public void setClick(getChageBack click) {
        this.click = click;
    }
}
